﻿// Process HTTP String Command
using System;
using System.Collections.Generic;
using System.Text;
using Jayrock.Json.Conversion;
using CyberOPCSpecification;

// Include the classes structure of the Specification(the same class for Client and Server

namespace CyberOPCClient
{
    public class CyberOPCClientCommands
    {

        // HTTP Request with GET
        // RequestGET = MakeHeader + http://<end point>?method=<method>&params=<params>
        // Encoding Steps:
        //  Base 64 param value
        //  URL Encoded Base 64 param value
        //  Pre-Encoded Params:
        //  http://<end point>?method=sum&params={"a":3,"b":4}&id=2
        //  http://<end point>?method=sum&params=[3,4]&id=1
        //  Encoded Request:
        //  http://<end point>?method=sum&params=eyJhIjozLCJiIjo0fQ%3D%3D&id=2
        //  http://<end point>?method=sum&params=WzMsNF0%3D&id=1
        //  NOTE: http://groups.google.com/group/json-rpc/web/json-rpc-over-http?pli=1

        private CyberOPCClientCommands() { }

        #region Public Methods

        public static string BuildRequest(string RPCmethod, object RPCparams, string HttpMethod)
        {
            StringBuilder RequestStrBld = new StringBuilder();
            string method = null;
            string parameters = null;
            string id = null;
            // NOTE: Import/Export JSON http://groups.google.com/group/jayrock/web/import-export-json-data
            // Or into a String :
             //StringBuilder sb = new StringBuilder(); 
             //JsonConvert.Export( myObject, sb);
            //or
            //  String s = JsonConvert.ExportToString( myObjet );
            switch (RPCmethod)
            {
                case "GetStatus":
                    method = "GetStatus";
                    parameters = "";
                    id = "1";
                    break;
                case "Browse":
                    method = "Browse";
                    parameters = (string)RPCparams;
                    id = "2";
                case "Read":
                    method = "Read";
                    parameters = ((ItemList[])RPCparams).ToString();
                    id = "3";
                    break;
                case "Write":
                    method = "Write";
                    parameters = ((WItemList[])RPCparams).ToString();
                    id = "4";
                    break;


            }
            if (HttpMethod == "GET")
            {
                #region NOTE JSON-RPC
                //Pre-Encoded Params:
                //http://<end point>?method=sum&params={"a":3,"b":4}&id=2
                //http://<end point>?method=sum&params=[3,4]&id=1
                //Encoded Request:
                //http://<end point>?method=sum&params=eyJhIjozLCJiIjo0fQ%3D%3D&id=2
                //http://<end point>?method=sum&params=WzMsNF0%3D&id=1
                #endregion
                RequestStrBld.AppendFormat("GET ?method={0}&params={1}&id={2} HTTP/1.1\r", method, System.Web.HttpUtility.UrlEncode(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(parameters))), id);
                RequestStrBld.AppendFormat("User-Agent: {0}\r", "cyberopcclient/1.6");
                RequestStrBld.AppendFormat("Host: {0}\r", "");
                RequestStrBld.Append("Accept: application/json\r");
                RequestStrBld.Append("\r\n\r\n");//CRLR
            }
            return RequestStrBld.ToString();
        }
       
        #endregion
    }
}
